package com.xinqi.common.satoken.config;

import cn.dev33.satoken.jwt.SaJwtUtil;
import cn.dev33.satoken.jwt.exception.SaJwtException;
import cn.dev33.satoken.stp.StpLogic;

import java.util.Map;

public class StpLogicJwtForSimple extends StpLogic {
    public StpLogicJwtForSimple() {
        super("login");
    }

    public StpLogicJwtForSimple(String loginType) {
        super(loginType);
    }

    public String jwtSecretKey() {
        String key = this.getConfig().getJwtSecretKey();
        SaJwtException.throwByNull(key, "请配置jwt秘钥", 30205);
        return key;
    }

    @Override
    public String createTokenValue(Object loginId, String device, long timeout, Map<String, Object> extraData) {
        return SaJwtUtil.createToken(this.loginType, loginId, device, timeout, extraData, this.jwtSecretKey());
    }

    @Override
    public Object getExtra(String key) {
        return this.getExtra(this.getTokenValue(), key);
    }

    @Override
    public Object getExtra(String tokenValue, String key) {
        return SaJwtUtil.getPayloadsNotCheck(tokenValue, this.loginType, this.jwtSecretKey()).get(key);
    }

    @Override
    public boolean getConfigOfIsShare() {
        return false;
    }
}
